﻿using Neudesic.Pulse.Entities;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace Neudesic.Pulse.ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            // Let's demonstrate using the Pulse client to interact with the Pulse services.
            string newline = Environment.NewLine;

            // This is the base Url for our Pulse services instance (set in config)
            string pulseServiceUrl = ConfigurationManager.AppSettings["PulseServiceUrl"];

            // PulseClient supports IDisposable, so you can implement a using pattern with it, if desired
            // Instantiate the client, passing in the service url and the authentication scheme.
            using (PulseClient client = new PulseClient(pulseServiceUrl, AuthenticationScheme.Ntlm))
            {

                // 1. Grab the current user
                Entity me = client.CurrentUser; // CurrentUser is always the Pulse entity identified through the authentication mechanism
                WL("You are interacting with Pulse as: {0}, Id: {1}", me.Name, me.Url);
                WL(newline);


                // 2. Post a status
                WL("Please enter a status for Pulse.");

                // Create a new status with the input
                StatusRequest status = new StatusRequest()
                {
                    Text = RL()
                };

                // Post to Pulse
                client.CreateStatus(status, me);
                WL(newline);


                // 3. Grabbing status updates
                WL("Let's query your feed.  The first 10 items are:");

                // Grab the feed (a paged activity list) for an entity (you)
                PagedActivityList myFeed = client.GetPagedActivityList(me); // You can also pass in any other "entity" (tag, system, page, etc.) to get that entity's feed

                // Iterate through the first 10 and emit to the console.
                if (myFeed != null)
                {
                    foreach (var item in myFeed.Activities.Take(10))
                    {
                        WL("{0} said: {1}", item.Author.Name, item.Text);
                    }
                }
                WL(newline);


                // 4. Find out who's following me.
                // This is not supported by the services today.


                // 5. Find out who I'm following.
                WL("Let's query your user subscriptions.  The first 10 \"following\" are:");

                // Grab the list of people you are following
                List<Entity> following = client.GetSubscriptions(me, EntityType.Person); // Can also just call GetSubscriptions(me) to get all subscriptions to people, tags, systems, pages, etc.

                // Iterate the first 10 and emit to console.
                if (following != null)
                {
                    foreach (var item in following.Take(10))
                    {
                        WL("You are following {0}.", item.Name);
                    }
                }
                WL(newline);


                // 6. Search for a phrase.
                WL("Please enter a phrase to search for:");
                string phrase = RL();

                // Search for a phrase
                List<Activity> phraseResults = client.Search<Activity>(EntityType.Phrase, phrase); // searching for phrases return "activities"

                if (phraseResults != null)
                {
                    foreach (var item in phraseResults.Take(10))
                    {
                        WL("You found: {0} located at {1}.", item.Text, item.Url);                        
                    }
                }
                WL(newline);


                // 7. Search for a person.
                WL("Please enter a person to search for:");
                string person = RL();

                // Search for a person
                List<Entity> personResults = client.Search<Entity>(EntityType.Person, person); // searching for anything else (person, page, tag, system) returns "entities"

                if (personResults != null)
                {
                    foreach (var item in personResults.Take(10))
                    {
                        WL("You found: {0} located at {1}.", item.Name, item.Url);
                    }
                }
                WL(newline);

                WL("Done. Press <ENTER> to terminate.");
                Console.ReadLine();

            }

        }

        /// <summary>
        /// Helper function to read console input.
        /// </summary>
        /// <returns></returns>
        private static string RL()
        {
            return Console.ReadLine();
        }

        /// <summary>
        /// Helper function to emit formatted text to the console, using the list of arguments.
        /// </summary>
        /// <param name="format">The format.</param>
        /// <param name="args">The args.</param>
        private static void WL(string format, params object[] args)
        {
            WL(string.Format(format, args));
        }

        /// <summary>
        /// Helper function to emit text to the console.
        /// </summary>
        /// <param name="text">The text.</param>
        private static void WL(string text)
        {
            Console.WriteLine(text);
        }

    }
}
